package com.baidu.camera.ui;

import android.graphics.Point;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;

import com.baidu.camera.R;
import com.baidu.camera.utils.ToastUtils;
import com.baidu.camera.widget.CameraSurface;
import com.baidu.camera.widget.FocusImageView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.io.IOException;

@EActivity(R.layout.activity_surface_camera)
public class SurfaceCameraActivity extends AppCompatActivity implements SurfaceHolder.Callback, View.OnTouchListener {

    @ViewById
    CameraSurface surface;
    @ViewById
    FocusImageView icon;
    private Camera camera;
    private Camera.AutoFocusCallback autoFocusCallback;

    int zoom;

    @AfterViews
    void init() {
        surface.getHolder().addCallback(this);
        surface.setOnTouchListener(this);
        camera = Camera.open();
        camera.setDisplayOrientation(90);

        //
        autoFocusCallback = new Camera.AutoFocusCallback() {
            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                if (success) {
                    icon.onFocusSuccess();
                    ToastUtils.simpleToast(getApplicationContext(), "focus success");
                } else {
                    icon.onFocusFailed();
                    ToastUtils.simpleToast(getApplicationContext(), "focus fail");
                }
            }
        };
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            camera.setPreviewDisplay(holder);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (camera != null) {
            camera.stopPreview();
            camera.release();
        }
    }

    // touch then focus automatic
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            Point point = new Point((int) event.getX(), (int) event.getY());
            icon.startFocus(point);
//            PrintUtils.p("x=" + point.x + "  y=" + point.y);
            surface.onFocus(point, camera, autoFocusCallback);
        }
        return true;
    }

    @Click({R.id.add, R.id.decrease})
    void CLick(View view) {
        switch (view.getId()) {
            case R.id.add:
                resetZoom(++zoom);
                break;
            case R.id.decrease:
                resetZoom(--zoom);
                break;
        }
    }

    private void resetZoom(int i) {
        Camera.Parameters parameters = camera.getParameters();
        if (parameters.isZoomSupported()) {
            parameters.setZoom(i);
            camera.setParameters(parameters);
        }
    }
}
